package org.openkoala.security.application.impl;

import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.openkoala.security.application.UserApplication;
import org.openkoala.security.core.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.transaction.annotation.Transactional;

@Named
@Transactional
public class UserApplicationImpl implements UserApplication {
    @Autowired  
    private NamedParameterJdbcTemplate jdbcTempSeverice ; 
    
    @Autowired  
    private JdbcTemplateObj jdbcTemplateObj ; 
	@Override
	public void saveUser(User user) {
		user.save();
	}
	@Override
	public void grantRolesToUser(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		StringBuffer sqlRole = new StringBuffer() ;
		sqlRole.append(" insert into  KS_AUTHORIZATIONS au (au.id,au.version,au.actor_id,au.authority_id) \n") ;
		sqlRole.append(" values (HIBERNATE_SEQUENCE.nextval,:version,:actorId,:authority) \n") ;
		jdbcTempSeverice.update(sqlRole.toString(), condition) ;
	}
	@Override
	public void deleteUser(User userO) {
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(userO);
		// 删除权限
		String sqlDelRole = "delete from KS_AUTHORIZATIONS t where t.actor_id = :id" ;
		jdbcTempSeverice.update(sqlDelRole, paramSource) ;
		// 删除用户信息、
		String sqlDelUser = "delete from KS_ACTORS t where t.id = :id" ;
		jdbcTempSeverice.update(sqlDelUser, paramSource) ;
	}
	@Override
	public void grantDepToUser(User user) {
		// TODO Auto-generated method stub
		String sql = "update ks_actors actors set actors.dep_yb = :depYb where actors.id = :id  " ;
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
		jdbcTempSeverice.update(sql, paramSource) ;
	}
	@Override
	public void grantWardToUser(User user) {
		// TODO Auto-generated method stub
		String sql = "update ks_actors actors set actors.ward_code = :wardCode where actors.id = :id " ;
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(user);
		jdbcTempSeverice.update(sql, paramSource) ;
	}
	/* (non-Javadoc)
	 * @see org.openkoala.security.application.UserApplication#getPassword(java.lang.String)
	 */
	@Override
	public String getPassword(String username) {
		String sql = "select password from drgs_hosp_koala.ks_actors where user_account='" + username + "' and DISABLED=0 ";
		String password = jdbcTemplateObj.queryForObject(sql, String.class);
		//System.out.println("casFilter password: "+password);
		return password;
		
	}



	@Override
	public Long queryUserId(User user) {
		Long id=jdbcTemplateObj.queryForObject("select id  from KS_ACTORS t where t.user_account='"+user.getUserAccount()+"'",Long.class);
		return id ;
	}
}
